var config = require('../../../config/config.js');
var list = [];
Page({
  /**
   * 页面的初始数据
   */
  data: {
    address: list,
    getaddress: {},
    aid: 1,
    mid: 1,
    sid: 1,
    toshow: 0,
    is_default: 0,
    flag: '',
    reback: 2,
  },
  // 编辑
  editClick: function (e) {
    var id = e.currentTarget.dataset.id;
    var name = e.currentTarget.dataset.name;
    var mobile = e.currentTarget.dataset.mobile;
    var address = e.currentTarget.dataset.address;
    var area_info = e.currentTarget.dataset.area_info;
    var area_id = e.currentTarget.dataset.area_id;
    wx.navigateTo({
      url: '../address-edit/address-edit?name=' + name + '&mobile=' + mobile + '&address=' + address + '&area_info=' + area_info + '&toshow=' + '1' + '&type=' + '1' + '&id=' + id + '&area_id=' + area_id,
    })
    var that = this;
    var member_info = (wx.getStorageSync('member_info'));
    wx.request({
      url: config.address_edit,
      data: {
        mid: member_info.mid,
        sid: member_info.sid,
        aid: member_info.id,
        id: e.currentTarget.dataset.id
      },
      method: 'GET',
      success: function (res) {
      },
      fail: function () {
        wx.showToast({
          title: '服务器网络错误!',
          icon: 'loading',
          duration: 1500
        })
      }
    })
  },
  //删除当前行
  deleteClick: function (e) {
    var that = this;
    var member_info = (wx.getStorageSync('member_info'));
    wx.request({
      url: config.address_delete,
      data: {
        id: e.currentTarget.dataset.id,
        name: e.currentTarget.dataset.name,
        mobile: e.currentTarget.dataset.mobile,
        address: e.currentTarget.dataset.address,
        area_info: e.currentTarget.dataset.area_info,
        aid: this.data.aid,
        mid: member_info.mid,
        sid: this.data.sid,
      },
      method: 'POST',
      success: function (res) {
        //把删除的一行从列表中删除
        var address = that.data.address;
        var index = e.currentTarget.dataset.index;
        address.splice(index, 1);
        that.setData({
          address: address
        })
        wx.showToast({
          title: "删除成功",
        });
      },
      fail: function () {
      },
    })
  },

  //添加收货地址 
  addClick: function (e) {
    if (this.data.address) {
      if (this.data.address.length > 4) {
        wx.showModal({
          title: '提示',
          content: '最多只能添加5条收货地址',
          showCancel: false
        })
        return
      }
    }
    wx.navigateTo({
      url: '../address-add/address-add',
    })
    var that = this;
    var member_info = (wx.getStorageSync('member_info'));
    wx.request({
      url: config.address_add,
      data: {
        aid: member_info.aid,
        mid: member_info.mid,
        sid: member_info.sid,
      },
      method: 'POST',
      success: function (res) {
      },
      fail: function () {
        wx.showToast({
          title: '服务器网络错误!',
          icon: 'loading',
          duration: 1500
        })
      }
    })
  },

  //设为默认地址
  radioClick: function (e) {
    var that = this;
    var member_info = (wx.getStorageSync('member_info'));
    wx.request({
      url: config.address_set_default,
      data: {
        id: e.currentTarget.dataset.id,
        name: e.currentTarget.dataset.name,
        mobile: e.currentTarget.dataset.mobile,
        address: e.currentTarget.dataset.address,
        area_info: e.currentTarget.dataset.area_info,
        aid: member_info.aid,
        mid: member_info.mid,
        sid: member_info.sid,
      },
      method: 'POST',
      success: function (res) {
        that.getaddressindexUrl()
        if (!res.data.status) {
          wx.showToast({
            title: '操作失败',
          })
        }
      },
      fail: function () {
      },
    })
  },

  //请求地址接口
  getaddressindexUrl: function (event) {
    var that = this;
    var member_info = (wx.getStorageSync('member_info'));
    wx.request({
      url: config.address_list,
      method: "GET",
      data: {
        aid: member_info.aid,
        mid: member_info.mid,
        sid: member_info.sid
      },
      success: function (res) {
        if (!res.data.data.address_list){
          return
        }
        that.setData({
          address: res.data.data.address_list
        })
      },
      fail: function (res) {
        console.log(res.data)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var member_info = (wx.getStorageSync('member_info'));
    this.setData({
      mid: member_info.mid
    }) 
    this.setData({
      reback: options.reback
    })
  },
  //返回上一级传回地址

  returnParent: function (e) {
    if (!this.data.address) {
      return
    }
    let pages = getCurrentPages();//当前页面
    let prevPage = pages[pages.length - 2];//上一页面
    var address = this.data.address;
    var index = e.currentTarget.dataset.index;
    for (var i in address) {
      if (i == index) {
        prevPage.setData({//直接给上移页面赋值
          address: this.data.address[i],
        })
      }
    }
    var reback = this.data.reback
    if (reback == 0) {
      wx.navigateBack({
        delta: 1,
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  },

  /**
   * 生命周期函数--监听页面显示
   */

  onShow: function () {
    this.getaddressindexUrl()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})